<?php
class Tourismprovider extends BZ_Controller
{
    public function _init()
    {
        $this->load->model('tourism_collect_shop_model','tourism_collect_shop');
        $this->load->model('tourism_provider_category_model','tourism_provider_category');
        $this->load->model('tourism_provider_model','tourism_provider');
        $this->load->model('tourism_goods_base_model','tourism_goods_base');
        $this->load->model('tourism_key_word_model', 'tourism_key_word');
        $this->load->model('advert_model', 'advert');
        $this->load->model('tourism_headline_model', 'tourism_headline');
    }

    /**
     * 站点页面
     */
    public function grid()
    {
        $providerUid = $this->providerUid;//trim($this->input->get('providerUid'));
        $this->data['providerInfor'] = $this->provider;
        $this->data['providerCategory'] = $this->tourism_provider_category->providerCategoryByUid($providerUid);
        $this->data['advert_img'] = $this->advert->findByParams(['provider_id' => $providerUid, 'status'=> 1, 'opentype' => 'link', 'position' => 1], '*', null, 'sort ASC')->result(); //广告
        //$this->data['collectTotal'] = $this->tourism_collect_shop->collectProviderTotal($providerUid);
        //$this->data['isCollect'] = empty( $this->uid ) ? false : $this->tourism_collect_shop->isCollectProviderByUid($this->uid, $providerUid);
        $this->data['mainProduct'] = $this->tourism_goods_base->findProductByUidAndCategory(['uid' => $providerUid, 'ismain' => true], $pageNum = 9, $num = 0);//特价产品
        $this->data['recommendProduct'] = $this->tourism_goods_base->findProductByUidAndCategory(['uid' => $providerUid, 'sort' => 4], $pageNum = 8, $num = 0);//推荐产品
        $this->data['keyWordList'] = $this->tourism_key_word->findByParams(['provider_id' => $this->providerUid, 'status' => 1]);
        $this->data['providerUid'] = $providerUid;
        $this->data['uid'] = $this->uid;
        $this->load->view('tourism/tourismprovider/grid', $this->data);
    }
    
    /**
     * ajax刷收藏站点
     **/
    public function ajaxCollectShop()
    {
        $providerUid = $this->providerUid;
        $result = $this->tourism_collect_shop->isCollectProviderByUid($this->uid, $providerUid);
        if( $result ){
            echo json_encode(array('status'=>false,'content'=>"已收藏"));exit;
        }
        $param = array('userUid'=>$this->uid,'providerUid'=>$providerUid);
        $status = $this->tourism_collect_shop->insertCollectShop($param);
        echo $status ? json_encode(array('status'=>true,'content'=>'已收藏')) : json_encode(array('status'=>false,'content'=>'收藏失败'));exit;
    }

    /**
     * ajax刷新翻页 ==供应商商品
     * @param int $pg
     */
    public function ajaxProviderProduct($pg = 1)
    {
        $page_num = 6;
        $num = ($pg - 1) * $page_num;
        $param = ['uid' => $this->providerUid];
        $data['provider_product'] = $this->tourism_goods_base->findProductByUidAndCategory($param, $page_num, $num);
        $this->ajaxJson(['status' => true, 'html' => $this->load->view('tourism/tourismprovider/providerlist', $data, true)]);
    }

    /**
     * 该商铺所有商品
     */
    public function allProduct()
    {
        $param = $this->input->get();
        $providerUid = $this->providerUid;
        $param['uid'] = $providerUid;
        $param['sort'] = 6;
        $this->data['providerCategory'] = $this->tourism_provider_category->providerCategoryByUid($providerUid);
        $this->data['recommendProduct'] = $this->tourism_goods_base->findProductByUidAndCategory($param, $pageNum=12, $num=0);//推荐产品
        $this->data['keyWordList'] = $this->tourism_key_word->findByParams(['provider_id' => $providerUid, 'status' => 1]);
        $this->data['providerUid'] = $providerUid;
        $this->data['sort'] = array_key_exists('sort', $param) ? $param['sort'] : '';
        $this->data['keyword'] = array_key_exists('keyword',$param) ? $param['keyword'] : '';
        $this->data['category_id'] = array_key_exists('category_id',$param) ? $param['category_id'] : '';
        $this->load->view('tourism/tourismprovider/allproduct', $this->data);
    }

     /**
      * ajax加载所有商品
      */
    public function ajaxMoreProduct()
    {
        $pg  = $this->input->get('pg') ? $this->input->get('pg') : 2;
        $pageNum = 12;
        $num = ($pg-1)*$pageNum;
        $param = $this->input->get();
        $param['uid'] = $this->providerUid;
        $recommendProduct = $this->tourism_goods_base->findProductByUidAndCategory($param, $pageNum, $num);
        $this->data['recommendProduct'] = $recommendProduct;
        echo ($recommendProduct->num_rows() > 0) ? json_encode(array(
           'status'  => true,
           'num'     => $recommendProduct->num_rows(),
           'content' => $this->load->view('tourism/tourismprovider/list', $this->data, true)
        )):json_encode(array(
           'status'  => false,
        ));exit;
    }

    /**
     * 简介
     */
    public function intro()
    {
        $providerInfor = $this->tourism_provider->findOne(['uid' => $this->providerUid, 'flag' => 1]);
        if ($providerInfor->num_rows() <= 0) {
            $this->alertError('该供应商不存在');
        }
        $this->data['providerInfor'] = $providerInfor->row(0);
        $this->load->view('tourism/tourismprovider/intro', $this->data);
    }

    public function news()
    {
        $result = $this->tourism_headline->findByParams(['provider_id' => $this->providerUid, 'status' => 1], '*', null, 'sort ASC');
        $this->data['headline'] = $result->result();
        $this->load->view('tourism/tourismprovider/news', $this->data);
    }

    public function newsInfo($id)
    {
        $result = $this->tourism_headline->findOne(['id' => $id, 'status' => 1], '*');
        $this->data['headline']  = $result->row();
        $this->data['headTitle'] = $this->data['headline']->title;
        $this->load->view('tourism/tourismprovider/newsInfo', $this->data);
    }

    public function search()
    {
        $this->data['providerUid'] = $this->providerUid;
        $this->data['keyWordList'] = $this->tourism_key_word->findByParams(['provider_id' => $this->providerUid, 'status' => 1]);
        $this->load->view('tourism/tourismprovider/searchsigle', $this->data);
    }
}